//
//  ListenPageViewController.m
//  SuperNews
//
//  Created by qiang on 16/1/9.
//  Copyright © 2016年 qiang. All rights reserved.
//

#import "ListenPageViewController.h"
#import "ListenViewController.h"
#import "VideoViewController.h"

@interface ListenPageViewController () <UIScrollViewDelegate>

@property (nonatomic, strong) UISegmentedControl *segmentC;
@property (nonatomic, strong) ListenViewController *listenVC;
@property (nonatomic, strong) VideoViewController *videoVC;
@property (nonatomic, strong) UIScrollView *scrollV;

@end

@implementation ListenPageViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    for (UIView *view in self.navigationController.navigationBar.subviews) {
        if ([view isKindOfClass:[UISegmentedControl class]]) {
            view.hidden = NO;
        }
    }
    self.navigationController.navigationBarHidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.segmentC = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"视频", @"电台", nil]];
    self.segmentC.center = CGPointMake(([UIScreen mainScreen].bounds.size.width - 100) / 2.0, 24);
    self.segmentC.tintColor = [UIColor whiteColor];
    self.segmentC.backgroundColor = [UIColor colorWithRed:150.0 / 255.0 green:10.0 / 255.0 blue:0.0 alpha:1.0];
    [self.segmentC addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    [self.segmentC setWidth:100 forSegmentAtIndex:0];
    [self.segmentC setWidth:100 forSegmentAtIndex:1];
    self.segmentC.selectedSegmentIndex = 0;
    [self.navigationController.navigationBar addSubview:self.segmentC];
    
    self.listenVC = [[ListenViewController alloc] init];
    [self.listenVC.view setFrame:CGRectMake([UIScreen mainScreen].bounds.size.width, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    [self addChildViewController:self.listenVC];
    
    self.videoVC = [[VideoViewController alloc] init];
    [self.videoVC.view setFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    [self addChildViewController:self.videoVC];
    
    self.scrollV = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    self.scrollV.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width * 2.0, 0);
    self.scrollV.bounces = YES;
    self.scrollV.pagingEnabled = YES;
    self.scrollV.delegate = self;
    [self.view addSubview:self.scrollV];
    [self.scrollV addSubview:self.videoVC.view];
    [self.scrollV addSubview:self.listenVC.view];
}

- (void)segmentAction:(UISegmentedControl *)sender
{
    if (0 == sender.selectedSegmentIndex) {
        self.scrollV.contentOffset = CGPointMake(0, 0);
    } else if (1 == sender.selectedSegmentIndex) {
        self.scrollV.contentOffset = CGPointMake([UIScreen mainScreen].bounds.size.width, 0);
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    self.segmentC.selectedSegmentIndex = (self.scrollV.contentOffset.x + [UIScreen mainScreen].bounds.size.width * 0.5) / [UIScreen mainScreen].bounds.size.width;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
